package game.cube;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.Random;

public class Point implements Shape {
	private static final int FLOAT_SIZE_BYTES	= 4;

	public static  int   MAX_TIME		= 60 * 1;
	public static  int   MAX_DIV		= 33;
	public static  int   MAX_NUMBER		= MAX_DIV * MAX_DIV * MAX_DIV;
	public static  float BACKGROUND_COLOR = 0.3f;
	public float[] mVertex = new float[MAX_NUMBER * 3];
	public float[] mColor  = new float[MAX_NUMBER * 4];
	
	public float[] mMove = new float[MAX_NUMBER * 3];
	public float[] mMoveColor = new float[MAX_NUMBER * 4];
	
	private Matrix			 mMixedMatrix = new Matrix();

	private FloatBuffer mVertexBuffer;
	private FloatBuffer  mColorBuffer;
	private ShortBuffer  mIndexBuffer;

	private	Random mRandom = new Random();
	int mCount;

	public Point() {
		initialize();
	}
	
	public void initialize() {
		mCount = 0;
		for (int i = 0; i < MAX_NUMBER * 3; ++i) {
			mMove[i] = (mRandom.nextInt(MAX_DIV) - (MAX_DIV - 1) / 2 ) * 0.01f;
		}
		int i = 0;
		for (int x = 0; x < MAX_DIV; ++x) {
		for (int y = 0; y < MAX_DIV; ++y) {
		for (int z = 0; z < MAX_DIV; ++z) {
			mMoveColor[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 4 + 0] = ((x / (float)MAX_DIV) - BACKGROUND_COLOR) / MAX_TIME;
			mMoveColor[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 4 + 1] = ((y / (float)MAX_DIV) - BACKGROUND_COLOR) / MAX_TIME;
			mMoveColor[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 4 + 2] = ((z / (float)MAX_DIV) - BACKGROUND_COLOR) / MAX_TIME;
			
			mColor[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 4 + 0] = BACKGROUND_COLOR;
			mColor[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 4 + 1] = BACKGROUND_COLOR;
			mColor[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 4 + 2] = BACKGROUND_COLOR;
			mColor[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 4 + 3] = BACKGROUND_COLOR;
			mVertex[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 3 + 0] = (x - MAX_DIV / 2) * 0.15f + mMove[i * 3 + 0] * MAX_TIME;
			mVertex[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 3 + 1] = (y - MAX_DIV / 2) * 0.15f + mMove[i * 3 + 1] * MAX_TIME;
			mVertex[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 3 + 2] = (z - MAX_DIV / 2) * 0.15f + mMove[i * 3 + 2] * MAX_TIME;
			++i;
		}
		}
		}
		mVertexBuffer = ByteBuffer.allocateDirect(mVertex.length * FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
		mVertexBuffer.put(mVertex).position(0);

		mColorBuffer = ByteBuffer.allocateDirect(mColor.length * FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
		mColorBuffer.put(mColor).position(0);

		mMixedMatrix.identity();		
	}
	
	@Override
	public void setTranslate(float x, float y, float z) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void add_translation(float x, float y, float z) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setParentTranslate(float x, float y, float z) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void add_parent_translation(float x, float y, float z) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setRotateX(float angle) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addRotateX(float angle) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mixParentTranslate() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mixTranslateToRotateMatrix() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mixRotateToTranslateMatrix() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean is_active() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public FloatBuffer getVertexBuffer() {
		if (++mCount <= MAX_TIME) {
			for (int i = 0; i < MAX_NUMBER * 3; ++i) {
				mVertex[i] -= mMove[i];
			}
		
			mVertexBuffer.put(mVertex).position(0);
		}
		return mVertexBuffer;
	}

	@Override
	public ShortBuffer getIndexBuffer() {
		return mIndexBuffer;
	}

	@Override
	public FloatBuffer getColorBuffer() {
		if (mCount <= MAX_TIME) {
			for (int x = 0; x < MAX_DIV; ++x) {
			for (int y = 0; y < MAX_DIV; ++y) {
			for (int z = 0; z < MAX_DIV; ++z) {
				mColor[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 4 + 0] += mMoveColor[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 4 + 0];
				mColor[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 4 + 1] += mMoveColor[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 4 + 1];
				mColor[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 4 + 2] += mMoveColor[(x * MAX_DIV * MAX_DIV + y * MAX_DIV + z) * 4 + 2];
			}
			}
			}
			mColorBuffer.put(mColor).position(0);
		}
		return mColorBuffer;
	}

	@Override
	public int getIndexLength() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public float[] getMatrix() {
		// TODO Auto-generated method stub
		return mMixedMatrix.m;
	}
}
